home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / Text Capture FKEY / Select_rect.c < prev    next >
Text File  |  1992-03-15  |  2KB  |  99 lines

  1.  
  2. void Select_rect( Rect *rect );
  3.  
  4. /*
  5.     Select a rectangle from the screen, in global coordinates.
  6. */
  7.  
  8.  
  9. static void Make_rgn( Point one, Point two, RgnHandle new_rgn );
  10.  
  11. static Pattern my_gray ={0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
  12.  
  13. /* ------------------------ Select_rect ----------------------------- */
  14. void Select_rect( Rect *rect )
  15. {
  16.     GrafPtr            save_port;
  17.     GrafPort        wide_open;
  18.     CursHandle        cross_hair_h;
  19.     Cursor            crosshair;
  20.     EventRecord        event;
  21.     RgnHandle        old_rgn, new_rgn, diff_rgn;
  22.     Point            anchor_pt, old_pt, new_pt;
  23.     
  24.     old_rgn = NewRgn();
  25.     new_rgn = NewRgn();
  26.     diff_rgn = NewRgn();
  27.  
  28.     /*
  29.         Create a wide open GrafPort in which to draw on all screens.
  30.     */
  31.     GetPort( &save_port );
  32.     OpenPort( &wide_open );
  33.     CopyRgn( GetGrayRgn(), wide_open.visRgn );
  34.     wide_open.portRect = (**wide_open.visRgn).rgnBBox;
  35.     PenMode( patXor );
  36.     PenPat( my_gray );
  37.  
  38.     cross_hair_h = GetCursor( crossCursor );
  39.     crosshair = **cross_hair_h;
  40.     InitCursor();    // make sure the cursor is visible
  41.     SetCursor( &crosshair );
  42.     
  43.     /*
  44.         I use GetOSEvent instead of GetNextEvent so that I won't have to
  45.         worry about the mouse click causing a process switch.
  46.     */
  47.     while (!GetOSEvent( mDownMask, &event ))    // wait for a mouse click
  48.     {
  49.         SetCursor( &crosshair );
  50.     }
  51.     
  52.     GetMouse( &anchor_pt );        //  this is one corner of the rectangle
  53.     old_pt = anchor_pt;
  54.     Make_rgn( anchor_pt, old_pt, old_rgn );
  55.  
  56.     while (StillDown())            // track the mouse
  57.     {
  58.         GetMouse( &new_pt );
  59.         if (!EqualPt( new_pt, old_pt ))
  60.         {
  61.             Make_rgn( anchor_pt, new_pt, new_rgn );
  62.             XorRgn( old_rgn, new_rgn, diff_rgn );
  63.             PaintRgn( diff_rgn );        // in patXor mode
  64.             old_pt = new_pt;
  65.             CopyRgn( new_rgn, old_rgn );
  66.         }
  67.     }
  68.     PaintRgn( new_rgn );            // erase the rectangle
  69.     *rect = (**new_rgn).rgnBBox;    // and return it
  70.     
  71.     SetPort( save_port );
  72.     ClosePort( &wide_open );
  73.     InitCursor();
  74.     DisposeRgn( new_rgn );
  75.     DisposeRgn( old_rgn );
  76.     DisposeRgn( diff_rgn );
  77. }
  78.  
  79. /* ------------------------ Make_rgn -------------------------------- */
  80. /*
  81.     Make a region that is a one pixel thick rectangular frame,
  82.     given two corners.
  83. */
  84. static void Make_rgn( Point anchor, Point cursor, RgnHandle new_rgn )
  85. {
  86.     Rect        work_rect;
  87.     RgnHandle    work_rgn = NewRgn();
  88.     
  89.     if (cursor.h > anchor.h) ++cursor.h;
  90.     if (cursor.v > anchor.v) ++cursor.v;
  91.     Pt2Rect( anchor, cursor, &work_rect );
  92.     RectRgn( new_rgn, &work_rect );
  93.     InsetRect( &work_rect, 1, 1 );
  94.     RectRgn( work_rgn, &work_rect );
  95.     DiffRgn( new_rgn, work_rgn, new_rgn );
  96.     
  97.     DisposeRgn( work_rgn );
  98. }
  99.